import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Solution {

    public static void huad(int[] array, int low, int high) {
        if (low <= high) {
            return;
        }
        int mid = (low + high) / 2;
        huad(array, low, mid);
        huad(array, mid + 1, high);
        merger(array, low, mid, high);
    }

    public static void merger(int[] array, int low, int mid, int high) {
        int i = low;
        int j = mid + 1;
        int[] temp = new int[high - low + 1];
        int index = 0;
        while (i <= mid && j <= high) {
            if (array[i] <= array[j]) {
                temp[index] = array[i];
                i++;
            } else {
                temp[index] = array[j];
                j++;
            }
            index++;
        }
        while (i <= mid) {
            temp[index] = array[i];
            i++;
            index++;
        }
        while (j <= mid) {
            temp[index] = array[j];
            j++;
            index++;
        }
        for (int k = 0; k < temp.length; k++) {
            array[low + k] = temp[k];
        }
    }

    public static boolean isPalindrome(String s) {
        if (s.isEmpty()) {
            return false;
        }
        Stack<Character> stack = new Stack<>();
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            stack.push(chars[i]);
        }
        for (int j = 0; j < chars.length; j++) {
            Character cha = stack.pop();
            if (chars[j] != cha) {
                return false;
            }
        }
        return stack.isEmpty();
    }

    public static String compressString(String str) {
        Map<Character, Integer> map = new LinkedHashMap<>();
        for (int i = 0; i < str.length(); i++) {
            if (map.containsKey(str.charAt(i))) {
                map.put(str.charAt(i), map.get(str.charAt(i)) + 1);
            } else {
                map.put(str.charAt(i), 1);
            }
        }
        StringBuilder build = new StringBuilder();
        for (Map.Entry<Character, Integer> entry : map.entrySet()) {
            build.append(entry.getKey()).append(entry.getValue());
        }
        return build.toString().length() > str.length() ? build.toString() : str;
    }

    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        Map<String, Map<String, List<Student>>> result  = new HashMap<>();
        Map<String, List<Student>> collect = list.stream().collect(Collectors.groupingBy(j -> j.userName));
        List<Student> list2 = list.stream().filter(x -> {
            if(x.userName.equals("A")) {
                return true;
            }
            return false;
        }).collect(Collectors.toList());
        for (Map.Entry<String, List<Student>> entry : collect.entrySet()) {
            String key = entry.getKey();
            List<Student> value = entry.getValue();
            Map<String, List<Student>> collect1 = value.stream().collect(Collectors.groupingBy(j -> j.sex));
            result.put(key,collect1);
        }
        System.out.println(compressString("aabcccccaaa"));
        //输出：a2b1c5a3
        System.out.println(compressString("abbccd")); //输出：abbccd。 abbccd"压缩后为"a1b2c2d1"，比原字符串长度更长
    }

    class Student {

        private String userName;

        private String sex;
    }
}
